# 这是一个示例 Python 脚本。

# 按 Shift+F10 执行或将其替换为您的代码。
# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。

import serial
import time


def print_hi(name):
    # 在下面的代码行中使用断点来调试脚本。
    print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。


class SpcSerial:
    name = "serialX"
    port = "/dev/ttyAMA0"
    baudrate = 115200
    serial = serial.Serial()

    def __init__(self, name = "serialX",
                 port = "COM3",
                 baudrate = 115200,
                 timeout = 1):
        self.name = name
        self.port = port
        self.baudrate = baudrate
        print(name," ",port, " baud=",baudrate)
        self.serial = serial.Serial(port, baudrate, timeout=timeout)
        return

    def __del__(self):
        print(f"'{self.name} >> {self.port}' is Delet\n")
        self.serial.__del__()
        return

    def Open(self):
        if self.serial.isOpen():
            pass
        else:
            self.serial.open()
        print(f"'{self.name} >> {self.port}' is Open\n")
        return

    def Close(self):
        if self.serial.isOpen():
            self.serial.close()
        else:
            pass
        print(f"'{self.name} >> {self.port}' is Close\n")
        return

    def Send(self,data = b"__?__"):
        # self.serial.write(data.encode("utf-8"))
        self.serial.write(data)
        return

    def ReadLine(self):
        # data = self.serial.readline()
        data = self.serial.read(128)
        dataArray = list(data)
        datalen = len(dataArray)
        return (data, datalen)
    
    def Read(self, len: int):
        data = self.serial.read(len)
        return data

    def Read_Byte2Str(self, len: int, encode='gbk'):
        data_str = self.serial.read(len).decode('gbk')
        return data_str

    def Read_StrList(self, line: int, encode='gbk', eol=b'\n'):
        ret_list = []

        while len(ret_list) < line:
            # data = self.serial.readline(eol=eol).decode(encode)
            try:
                data = self.serial.read_until(eol).decode(encode)
            except UnicodeDecodeError:
                err_tip = "[SpcSerial Error:UnicodeDecodeError]"
                if encode == 'utf-8':
                    data = self.serial.read_until(eol).decode("gbk")
                else:
                    data = self.serial.read_until(eol).decode("utf-8")    
                data = err_tip + data

            if len(data) > 0:
                ret_list.append(data)
            else:
                break

        return ret_list


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':

    uart_alog = SpcSerial(name = "alog", port="COM49", baudrate=115200)

    uart_dev.Open()
    uart_wifi.Open()

    while (True):
        (data_dev, data_len) = uart_dev.ReadLine()
        if (data_len > 0):
            uart_wifi.Send(data_dev)
            print("dev-->wifi recv {0} Btye >> {1}".format(data_len, data_dev))

        (data_dev, data_len) = uart_wifi.ReadLine()
        if (data_len > 0):
            uart_dev.Send(data_dev)
            print("wifi-->dev recv {0} Btye >> {1}".format(data_len, data_dev))
        time.sleep(0.01)





# 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助
